import Moment from 'moment'
var util = {}

util.formatToTime = (time)=>{
    var date = new Date(time*1000) // 传入的时间单位为秒，需要转换为毫秒

    const year = date.getFullYear()
    const month = (date.getMonth()+1).toString().padStart(2,'0')
    const day = date.getDate().toString().padStart(2,'0')
    const hour = date.getHours().toString().padStart(2,'0')
    const minute = date.getMinutes().toString().padStart(2,'0')
    const second = date.getSeconds().toString().padStart(2,'0')

    return `${year}-${month}-${day} ${hour}:${minute}:${second}`
}

util.formatToDate = (time)=>{
    var date = new Date(time*1000) // 传入的时间单位为秒，需要转换为毫秒

    const year = date.getFullYear()
    const month = (date.getMonth()+1).toString().padStart(2,'0')
    const day = date.getDate().toString().padStart(2,'0')

    return `${year}-${month}-${day}`
}

util.format = (val, pattern = 'yyyy-MM-DD') =>{
    return Moment(val).format(pattern)
}

util.nodeTypeStr = (type) => {
    let typeStr = ''
    switch (type) {
        case 'start': typeStr = '开始节点'; break
        case 'copyto': typeStr = '抄送节点'; break
        case 'approve': typeStr = '审批节点'; break
        case 'end': typeStr = '结束节点'; break
        default:; break
    }
    return typeStr
}

// 验证是否是数字，支持小数
util.isDecimal=(rule, value, callback) =>{
    if (!value) {
        return callback();
    }
    setTimeout(() => {
        if (!Number(value)) {
            callback(new Error('请输入数字'));
        } else {
            callback();
        }
    }, 100);
}


/* 是否手机号码*/
util.validatePhone=(rule, value, callback) =>{
    const reg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
    if (value == '' || value == undefined || value == null) {
        callback();
    } else {
        if ((!reg.test(value)) && value != '') {
            callback(new Error('请输入正确的电话号码'));
        } else {
            callback();
        }
    }
}

/* 是否身份证号码*/
util.validateIdNo=(rule, value, callback) =>{
    const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    if (value == '' || value == undefined || value == null) {
        callback();
    } else {
        if ((!reg.test(value)) && value != '') {
            callback(new Error('请输入正确的身份证号码'));
        } else {
            callback();
        }
    }
}

/* 是否邮箱*/
util.validateEmail=(rule, value, callback) =>{
    const reg = /^[A-Za-z0-9-._]+@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,6})$/;
    if (value == '' || value == undefined || value == null) {
        callback();
    } else {
        if (!reg.test(value)) {
            callback(new Error('请输入正确的邮箱地址'));
        } else {
            callback();
        }
    }
}

util.parseNumber = (val, defaultValue = 0) => {
    if (!val) return defaultValue
    var newVal = Number(val)
    if (newVal == NaN) { return defaultValue }
    return newVal
}

util.contains = (arr, obj) => {
    var i = arr.length;
    while (i--){
        if(arr[i] == obj){
            return true;
        }
        if(i<0) break;
    }
    return false;
}
export default util
